<?php
/**
 * 共用模块，用户无需登录
 * Created by PhpStorm.
 * User: yuliang
 * Date: 2017/6/1
 * Time: 上午11:55
 */

namespace app\webapi\controller;


use app\common\R;
use think\Cache;
use think\Controller;
use think\Db;
use \PHPMailer\PHPMailer\PhpMailer;

class Tool extends Controller
{
    /**
     * 城市三级联动
     * @return \think\response\Json
     */
    public function areas(){
        $cacheAreas = Cache::get('areas');
        if($cacheAreas){
            return api_return('操作成功！',$cacheAreas);
        }else{
            $areas = Db::name('areas')->where('area_type','NEQ',0)->select();
            $areaTree = make_tree($areas);
            Cache::set('areas',$areaTree);
            return api_return('操作成功！',$areaTree);
        }
    }

    /**
     * 首页banner
     * @return \think\response\Json
     */
    public function banner(){
        $type = input('get.type');
        $validate = $this->validate(['type'=>$type],['type'=>'number']);
        if($validate !== true){
            return api_error('1000',$validate);
        }
        $banner = Db::name('slide')->where('cid',$type)->where('status',1)->order('listorder desc')->select();
        return api_return('获取banner成功！',$banner);
    }

    public function test(){
        echo ROOT_PATH;
        $a = input('a');
        $b = input('b');
//        $c = exec("/usr/bin/python /data/wwwroot/apichuanchuantv/public/bindingAccount.py $a $b");
//        $c = exec("/usr/bin/python /data/wwwroot/apichuanchuantv/public/pythonFiles/CCMain.py $a $b");
        $c = exec("/usr/bin/python /data/sftp/wenmingzhao/pythonFiles/CCMain.py $a $b");
    }

    // 作者排行榜
    public function noList(){
        $pt = input('pt')?input('pt'):true;
        $platform = input('platform')?input('platform'):true;
        $type = input('type')?input('type'):true;
        $category = map_sort(input('category'));
//        $pt = input('get.pt')?input('get.pt'):true;
//        $platform = input('get.platform')?input('get.platform'):true;
//        $type = input('get.type');
//        $category = map_sort(input('get.category'));

        $test1=Db::connect("db_mongo")->name("pgc_rank")->field('_id',true)->where('pt',$pt)->where('platform',$platform)
            ->where('type',$type)->where('category',$category)->find();
//        var_dump($test1);
        return api_return('操作成功！',$test1);
    }

    // 视频排行榜
    public function videoList(){

        $pt = input('pt')?input('pt'):true;
        $platform = input('platform')?input('platform'):true;
        $period = intval(input('period'))?intval(input('period')):true;
        $category = map_sort(input('category'));

        $result = Db::connect("db_mongo")->name("video_rank")->field('_id',true)->where('pt',$pt)->where('platform',$platform)
            ->where('period',$period)->where('category',$category)->find();
        return api_return('操作成功！',$result);
    }

    public function test1() {


//        R::set('b', '333');
//        echo R::get('b');

        $key = 'test_exec';

        for( $i=0; $i<10; $i++) {
            R::rpush($key, $i);
        }
//        echo R::lrange($key);
        echo '</br>';
        for ($i=0; $i<10; $i++) {
            echo R::lpop($key) . '</br>';
        }

        do {
            $val = R::lpop($key);
            exec($val);


        } while(true);

    }

    // 发送邮件并记录日志
    public function pushEmail(){
        $attachment = ROOT_PATH.'public/uploads/BusinessPlan.pptx';
        $info = Db::name("email")->where("status =0")->order("id desc")->find();
        if(!empty($info)){
            $email = $info['email'];
            $result = $this->send_mail($email,'尊敬的用户','视频传传','这是我的商业计划书，请查阅',$attachment);
            if($result){
                $fileName = ROOT_PATH.'public/uploads/success.log';
                file_put_contents($fileName,$email,FILE_APPEND);
            }
        }else{
            $fileName = ROOT_PATH.'public/uploads/email_error.log';
            file_put_contents($fileName,'没有可用邮箱！',FILE_APPEND);
        }
    }


    // 放入队列
    public function mailList(){
        $list = Db::name("email")->field("email")->order("id asc")->select();
        foreach ($list as $key => $value){
            R::rpush('maillist',$value['email']);
        }
    }

    // 发送邮件
    public function send_mail($tomail, $name, $subject = '', $body = '', $attachment = null) {
        $mail = new PHPMailer();           //实例化PHPMailer对象
        $mail->CharSet = 'UTF-8';           //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
        $mail->IsSMTP();                    // 设定使用SMTP服务
        $mail->SMTPDebug = 0;               // SMTP调试功能 0=关闭 1 = 错误和消息 2 = 消息
        $mail->SMTPAuth = true;             // 启用 SMTP 验证功能
        $mail->SMTPSecure = 'ssl';          // 使用安全协议
        $mail->Host = "smtp.163.com"; // SMTP 服务器
        $mail->Port = 465;                  // SMTP服务器的端口号
        $mail->Username = "haibinketang@163.com";    // SMTP服务器用户名
        $mail->Password = "haibinluoji1405";     // SMTP服务器密码
        $mail->SetFrom('haibinketang@163.com', '视频传传');
        $replyEmail = '';                   //留空则为发件人EMAIL
        $replyName = '';                    //回复名称（留空则为发件人名称）
        $mail->AddReplyTo($replyEmail, $replyName);
        $mail->Subject = $subject;
        $mail->MsgHTML($body);
        $mail->AddAddress($tomail, $name);
        if($attachment){
            $mail->AddAttachment($attachment);
        }
        return $mail->Send() ? true : $mail->ErrorInfo;
    }

}